import axios from 'axios';
import { Toast } from 'vant';
let instance = axios.create({
    baseURL: 'http://1.14.239.98',
    timeout: 10000,
});

instance.interceptors.request.use(
    function(config) {
        Toast.loading({
            message: '加载中...',
            forbidClick: true,
            duration: 0,
        });
        let token = localStorage.getItem('h5-token');
        if (token) {
            config.headers['Authorization'] = `Bearer ${token}`;
        }
        return config;
    },
    function(error) {
        Toast.clear();
        return Promise.reject(error);
    }
);
instance.interceptors.response.use(
    function(response) {
        Toast.clear();
        if (response.data.code != 0 && response.data.code != 401) {
            Toast.fail();
        }
        return response.data;
    },
    function(error) {
        Toast.clear();
        return Promise.reject(error);
    }
);

export default instance;